package com.leo.view;

import com.leo.bean.MessageInfo;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @BelongsProject: leo-emailfx
 * @BelongsPackage: com.leo.view
 * @Author: wangss
 * @CreateTime: 2023-02-28  23:53
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
public class MessageContentView extends BorderPane {

    private GridPane topGridPane;
    private Text subjectText;
    private Label fromLabel;
    private Label timeLabel;
    private Label toLabel;

    private WebView webView;
    private WebEngine engine;

    private HBox attachmenthBox;

    public MessageContentView() {
        super();
        // 主题、发件人、收件人
        topGridPane = new GridPane();

        // 居中、组件水平距离和垂直距离
        topGridPane.setAlignment(Pos.CENTER_LEFT);
        topGridPane.setHgap(10);
        topGridPane.setVgap(10);
        // 内边距
        topGridPane.setPadding(new Insets(25, 25, 25, 25));

        // 给列设置样式
        ColumnConstraints columnConstraints = new ColumnConstraints();
        columnConstraints.setFillWidth(true);
        columnConstraints.setHgrow(Priority.NEVER);
        columnConstraints.setMaxWidth(150);
        topGridPane.getColumnConstraints().add(columnConstraints);

        ColumnConstraints columnConstraints1 = new ColumnConstraints();
        columnConstraints1.setFillWidth(true);
        columnConstraints1.setHgrow(Priority.NEVER);
        columnConstraints1.setMinWidth(150);
        topGridPane.getColumnConstraints().add(columnConstraints1);

        // 给每行设置样式
        RowConstraints rowConstraints = new RowConstraints();
        rowConstraints.setFillHeight(true);
        rowConstraints.setVgrow(Priority.NEVER);
        for (int i = 0; i < 5; i++) {
            topGridPane.getRowConstraints().add(rowConstraints);
        }

        // 主题
        subjectText = new Text();
        topGridPane.add(subjectText, 0, 0, 2, 1);
        subjectText.setFont(Font.font(16));
        // 发件人
        Label sendLabel = new Label("发件人:");
        topGridPane.add(sendLabel, 0, 1);
        fromLabel = new Label();
        topGridPane.add(fromLabel, 1, 1);
        // 时间
        Label dateLabel = new Label("时   间:");
        topGridPane.add(dateLabel, 0, 2);
        timeLabel = new Label();
        topGridPane.add(timeLabel, 1, 2);
        // 收件人
        Label receiveLabel = new Label("收件人:");
        topGridPane.add(receiveLabel, 0, 3);
        toLabel = new Label();
        topGridPane.add(toLabel, 1, 3);
        this.setTop(topGridPane);
        // 内容
        webView = new WebView();
        engine = webView.getEngine();
        this.setCenter(webView);
        // 附件
        attachmenthBox = new HBox();
        attachmenthBox.setSpacing(10);
        this.setBottom(attachmenthBox);
        this.setVisible(false);
    }

    public void refreshView(MessageInfo messageInfo) {
        try {
            attachmenthBox.getChildren().clear();
            subjectText.setText(messageInfo.getSubject());
            fromLabel.setText(messageInfo.getEmailFrom());
            timeLabel.setText(messageInfo.getSendTime());
            toLabel.setText(messageInfo.getEmailReceive());

            String readString = Files.readString(Path.of(messageInfo.getContentPath()));
            engine.loadContent(readString);
            if (1 == messageInfo.getIsAttachment()) {
                String attachmentPath = messageInfo.getAttachmentPath();
                String[] attachSplit = attachmentPath.split(";");
                for (String filePath : attachSplit) {
                    String fileName = filePath;
                    if (filePath.contains("/")) {
                        fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
                    } else if (filePath.contains("\\")) {
                        fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
                    }
                    fileName = fileName.substring(fileName.indexOf("_") + 1);

                    Label label = new Label(fileName);
                    attachmenthBox.getChildren().add(label);
                    label.setOnMouseClicked(event -> {
                        log.info("打开文件路径：{}", filePath);
                        File file = new File(filePath);
                        if (Desktop.isDesktopSupported()) {
                            try {
                                Desktop.getDesktop().open(file);
                            } catch (IOException e) {
                                log.error("打开文件出错 {}", e.getMessage());
                            }
                        } else {
                            log.info("系统不支持的文件类型");
                        }
                    });
                    label.hoverProperty().addListener((observable, oldValue, newValue) -> {
                        if (newValue) {
                            label.setStyle("-fx-text-fill: blue");
                        } else {
                            label.setStyle("-fx-text-fill: black");
                        }
                    });
                }
            }
            this.setVisible(true);
        } catch (Exception e) {
            log.error("邮件展示试图刷新失败 {}", e.getMessage());
        }
    }
}
